package com.szholly.pro.watergas_server.ps.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.pro.watergas_server.common.web.BaseController;
import com.szholly.pro.watergas_server.common.web.RequestUnit;
import com.szholly.pro.watergas_server.ps.service.SwDailyInspectionService;
import com.szholly.pro.watergas_server.ps.service.ExceptionService;
import com.szholly.pro.watergas_server.ps.service.SwDomainService;
import com.szholly.data.general.PageInfo;
import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;

@Controller
@RequestMapping("/SwDailyInspection/*")
public class SwDailyInspectionController extends BaseController { 

	@Autowired
	SwDomainService swDomainService;

	@Autowired
	SwDailyInspectionService swDailyInspectionService;

	@Autowired
	ExceptionService exceptionService;

	protected Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 获取日常巡检运维项-地表水
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getInspectionItems")
	public void getInspectionItems(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String result = swDomainService.getDailyInspectionItems();
		outString(result);
	}
	
	/**
	 * 获取日常巡检分类-地表水
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getInspectionType")
	public void getInspectionType(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String level = RequestUnit.getParameter("level");
		String result = swDomainService.getDomainJSON("日常巡检分类", level);
		outString(result);
	}
	
	/**
	 * 获取日常巡检列表-地表水
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/geDailyInspectionList")
	public void geDailyInspectionList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String, String> map = getParams();
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = swDailyInspectionService.getDailyInspectionList(map, pageInfo);
		String result = DataUtil.listToJSONObject(list, null, pageInfo).toString();
		outString(result);
	}

	/**
	 * 导出日常巡检-地表水
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportDoc")
	public void exportDoc(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String,String> params = getParams();
		//查出项少导致显示不对，增加分页多项就可以解决
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageIndex(1);
		pageInfo.setPageSize(100);
		pageInfo.setRefreshRowCount(true);
		Map<String, Object> root = new HashMap<String, Object>();
		List<RowBase> list = swDailyInspectionService.getDailyInspectionList(params, pageInfo);
	
		if(list!=null && list.size()>0){
			root.putAll(list.get(0));
			for(RowBase row:list){
				root.put(row.getSafeString("ITEM"), row.getSafeString("ITEM_VALUE"));
			}
		}

		outWord(root, "sw/dailyInspection.xml", "日常巡检维护记录表.doc");
	}
	
	/**
	 * 获取日常巡检运维项-供水
	 * 
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value = "/getSupplyWaterInspectionItems")
	public void getSupplyWaterInspectionItems(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		String result = swDomainService.getWaterDailyInspectionItems();
		outString(result);
	}
	/**
	 * 获取日常巡检列表-地表水
	 * @param resq
	 * @param res
	 * @throws Exception
	 */
	@RequestMapping(value="/getSupplyWaterDailyInspectionList")
	public  void getSupplyWaterDailyInspectionList(HttpServletRequest resq, HttpServletResponse res) throws Exception {
		Map<String, String> map = getParams();
		PageInfo pageInfo = DataUtil.getPageInfo();
		List<RowBase> list = swDailyInspectionService.getSupplyWaterDailyInspectionList(map, pageInfo);
		String result = DataUtil.listToJSONObject(list, null, pageInfo).toString();
		outString(result);
	}
	
	/**
	 * 获取请求参数
	 * @return
	 */
	private Map<String, String> getParams() {
		Map<String, String> map = new HashMap<String, String>();
		String task_code = RequestUnit.getParameter("TASK_CODE");
		if (StringUtil.IsNotNullOrEmpty(task_code)) {
			map.put("task_code", task_code);
		}
		String start = RequestUnit.getParameter("START");
		if (StringUtil.IsNotNullOrEmpty(start)) {
			map.put("start", start);
		}
		String end = RequestUnit.getParameter("END");
		if (StringUtil.IsNotNullOrEmpty(end)) {
			map.put("end", end);
		}
		String wwtp_id = RequestUnit.getParameter("WWTP_ID");
		if (StringUtil.IsNotNullOrEmpty(wwtp_id)) {
			map.put("wwtp_id", wwtp_id);
		}
		String flow_type = RequestUnit.getParameter("FLOW_TYPE");
		if (StringUtil.IsNotNullOrEmpty(flow_type)) {
			map.put("flow_type", flow_type);
		}
		String station_id = RequestUnit.getParameter("STATION_ID");
		if (StringUtil.IsNotNullOrEmpty(station_id)) {
			map.put("station_id", station_id);
		}
		String water_type = RequestUnit.getParameter("WATER_TYPE");
		if (StringUtil.IsNotNullOrEmpty(water_type)) {
			map.put("water_type", water_type);
		}
		String station_name = RequestUnit.getParameter("STATION_NAME");
		if (StringUtil.IsNotNullOrEmpty(station_name)) {
			map.put("station_name", station_name);
		}
		String pitem_name = RequestUnit.getParameter("PITEM_NAME");
		if (StringUtil.IsNotNullOrEmpty(pitem_name)) {
			map.put("pitem_name", pitem_name);
		}
		return map;
	}
}
